priority -50

global !p
# A overkill(dirty) table with automatic alignment feature
def create_table(snip):
	# retrieving single line from current string and treat it like tabstops count
	placeholders_string = snip.buffer[snip.line].strip()
	rows_amount = int(placeholders_string[0])
	columns_amount = int(placeholders_string[1])

	prefix_str = "from vimsnippets import display_width;"

	# erase current line
	snip.buffer[snip.line] = ""

	# create anonymous snippet with expected content and number of tabstops
	anon_snippet_title = "| "
	anon_snippet_delimiter = "|"
	for col in range(1, columns_amount+1):
		sync_rows = [x*columns_amount+col for x in range(rows_amount+1)]
		sync_str = ",".join(["t[{0}]".format(x) for x in sync_rows])
		max_width_str = "max(list(map(display_width, [" + sync_str + "])))"
		cur_width_str = "display_width(t[" + str(col) + "])"
		rv_val = "(" + max_width_str + "-" + cur_width_str + ")*' '"
		anon_snippet_title += "${" + str(col)  + ":head" + str(col)\
			+ "}`!p " + prefix_str + "snip.rv=" + rv_val + "` | "
		anon_snippet_delimiter += ":`!p " + prefix_str + "snip.rv = "\
			+ max_width_str + "*'-'" + "`-|"

	anon_snippet_title += "\n"

	anon_snippet_delimiter += "\n"
	anon_snippet_body = ""
	for row in range(1, rows_amount+1):
		body_row = "| "
		for col in range(1, columns_amount+1):
			sync_rows = [x*columns_amount+col for x in range(rows_amount+1)]
			sync_str = ",".join(["t[{0}]".format(x) for x in sync_rows])
			max_width_str = "max(list(map(display_width, [" + sync_str + "])))"
			cur_width_str = "display_width(t[" + str(row*columns_amount+col) + "])"
			rv_val = "(" + max_width_str + "-" + cur_width_str + ")*' '"
			placeholder = "R{0}C{1}".format(row, col)
			body_row += "${" + str(row*columns_amount+col)  + ":" + placeholder\
				+ "}`!p " + prefix_str + "snip.rv=" + rv_val + "` | "

		body_row += "\n"
		anon_snippet_body += body_row

	anon_snippet_table = anon_snippet_title + anon_snippet_delimiter + anon_snippet_body

	# expand anonymous snippet
	snip.expand_anon(anon_snippet_table)
endglobal

###################
# Text formatting #
###################

snippet *** "bold italics"
***${1:${VISUAL}}***$0
endsnippet

snippet /* "Comment"
<!-- ${1:${VISUAL}} -->$0
endsnippet

################
# Common stuff #
################
snippet cbl "Codeblock" b
\`\`\`
$1
\`\`\`
$0
endsnippet

snippet refl "Reference Link"
[${1:${VISUAL:Text}}][${2:id}]$0

[$2]:${4:https://${3:www.url.com}} "${5:$4}"
endsnippet

snippet fnt "Footnote"
[^${1:${VISUAL:Footnote}}]$0

[^$1]:${2:Text}
endsnippet

snippet detail "Disclosure"
<details${3: open=""}>
  ${1:<summary>${2}</summary>}$0
</details>
endsnippet

post_jump "create_table(snip)"
snippet "tb([1-9][1-9])" "Fancy table" br
`!p snip.rv = match.group(1)`
endsnippet

# =====================
# custom snippets start
# =====================
priority 10

snippet src
<img alt="${1:<<>>}" src="${2:<<>>}" width="${3:<<>>}%"/>
$0
endsnippet

snippet ||
| $1 | <<>> |
|-|-|
| <<>> | <<>> |
| <<>> | <<>> |
endsnippet

snippet |
| $1 | <<>> |
$0
endsnippet

snippet ,a "bold" wA
**$1** <<>>
endsnippet

snippet ,i "italics" wA
*$1* <<>>
endsnippet

snippet ,s "deleted string" wA
~~$1~~ <<>>
endsnippet

snippet ,c "inline code" wA
\`$1\` <<>>
endsnippet

snippet ,` "code blocks" bA
\`\`\`$1
<<>>
\`\`\`
endsnippet

snippet ,p "picture" wA
![$1](<<>>)<<>>
endsnippet

snippet ,l "link" wA
[$1](<<>>)<<>>
endsnippet

snippet ,1 "Section" bA
# $1
endsnippet
snippet ，1 "Section" bA
# $1
endsnippet

snippet ,2 "Sub Section" bA
## $1
endsnippet
snippet ，2 "Sub Section" bA
## $1
endsnippet

snippet ,3 "SubSub Section" bA
### $1
endsnippet
snippet ，3 "SubSub Section" bA
### $1
endsnippet

snippet ,4 "Paragraph" bA
#### $1
endsnippet
snippet ，4 "Paragraph" bA
#### $1
endsnippet

snippet ,5 "Paragraph" bA
##### $1
endsnippet
snippet ，5 "Paragraph" bA
##### $1
endsnippet

snippet ,6 "Sub Paragraph" bA
###### $1
endsnippet
snippet ，6 "Sub Paragraph" bA
###### $1
endsnippet

snippet ,m "check list" bA
- [ ] $1
endsnippet

snippet nsq "mermaid sqaure node" w
$1["\`$0\`"]
endsnippet

snippet nro "mermaid round node" w
$1("\`$0\`")
endsnippet

snippet nrh "mermaid rhombus node" w
$1{"\`$0\`"}
endsnippet

snippet a "mermaid arrow" w
--> $0
endsnippet

snippet ad "mermaid dotted arrow" w
-.-> $0
endsnippet

snippet at "mermaid arrow with text" w
--> |$0| <<>>
endsnippet

snippet adt "mermaid dotted arrow with text" w
-.-> |$0| <<>>
endsnippet

# ===================
# custom snippets end
# ===================
